package evil.wolf.business;


public class NumToWordsConverter {

	public String mangso[] = {"Không", "Một", "Hai", "Ba", "Bốn", "Năm", "Sáu", "Bảy", "Tám", "Chín"};
	
//	public  NumToWordsConverter(int num) {
//		
//			startConvert();
//		
//	}
	
//	public static void main(String args[]) {
//		NumToWordsConverter n2w = new NumToWordsConverter();
//		System.out.print(n2w.startConvert(42342));
//		
//	}
	
	public String startConvert(int num) {
		
		String ors = Integer.toString(num);
		if(ors.length() <= 9)
			return (docso(Integer.parseInt(ors)) );

		else {
			String a = ors.substring(0, ors.length() - 9);
			String b = ors.substring(ors.length() - 9, ors.length());
			return (catchuoi(a, b) );
		}
	}
	
	private String catchuoi(String a, String b){
		String chuoi = "";
		chuoi = docso(Integer.parseInt(a)) + " Tỷ" + docso(Integer.parseInt(b)) ;
		return chuoi;
	}
	
	public String dochangchuc(int so, boolean daydu){
		String chuoi = "";
		int chuc = so/10;
		int donvi = so%10;
		
		if(chuc > 1){
			chuoi = " " + mangso[chuc] + " Mươi";
			if(donvi == 1){
				chuoi += " Mốt";
			}
		}else if(chuc == 1){
			chuoi = " Mười";
			if(donvi == 1){
				chuoi += " Một";
			}
		}else if (daydu && donvi > 0){
			chuoi = " Lẻ";
		}
		
		
		if (donvi == 5 && chuc > 1){
			chuoi += " Lăm";
		} else if(donvi > 1 || (donvi == 1 && chuc == 0)){
			chuoi += " " + mangso [donvi];
		}
		return chuoi;
	}
	
	public String docblock (int so, boolean daydu){
		String chuoi = "";
		int tram = so / 100;
		so = so % 100;
		if(daydu || tram > 0){
			chuoi = " " + mangso[tram] + " Trăm";
			chuoi += dochangchuc(so, true);
		} else {
			chuoi = dochangchuc(so, false);
		}
		return chuoi;
	}
	
	public String dochangtrieu(int so, boolean daydu){
		String chuoi = "";
		int trieu = so/1000000;
		so = so % 1000000;
		if(trieu > 0){
			chuoi = docblock(trieu, daydu) + " Triệu";
			daydu = true;
		}
		int nghin = so/1000;
		so = so%1000;
		if(nghin>0){
			chuoi += docblock(nghin, daydu) + " Nghìn";
			daydu = true;
		}
		if(so>0){
			chuoi+=docblock(so, daydu);
		}
		return chuoi;
	}
	
	public String docso(int so){
		if(so == 0){
			return mangso[0];
		}
		
		String chuoi = "";
		String hauto = "";
		int ty = 0;
		do {
			ty = so%1000000000;
			so = so/1000000000;
			if(so>0){
				chuoi = dochangtrieu(ty, true) + hauto + chuoi;
			}
			else{
				chuoi = dochangtrieu(ty, false) + hauto + chuoi;
			}
			hauto = " Tỷ";
		}while (so>0);
		
		return chuoi;
	}
}